package cn.fl.ass.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssDealSellCostService;
import cn.fl.ass.transform.AssDealSellCostVOTransform;
import cn.fl.ass.vo.AssDealSellCostVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import fl.ass.facade.api.IAssTrailCarApi;
import fl.ass.facade.dto.AssDealSellCostDTO;

/**
 * @Description: 处置-变卖费用信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssDealSellCostServiceImpl implements IAssDealSellCostService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IAssDealSellCostService.class);
	
	@Autowired
	private IAssTrailCarApi assTrailCarApi;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	@Override
	public AssDealSellCostVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		//TODO 业务逻辑
		AssDealSellCostDTO dto = null;//this.自定义ApiService.selectById(id);
		return AssDealSellCostVOTransform.toVO(dto);
	}

	@Override
	public Long add(AssDealSellCostVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		//return this.assDealSellCostApiService.add(AssDealSellCostVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean batchAdd(List<AssDealSellCostVO> voList)
			throws BaseException {
		if(CollectionUtils.isEmpty(voList)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		List<AssDealSellCostDTO> dtoList = AssDealSellCostVOTransform.toDTOList(voList);
		return this.assTrailCarApi.batchAddAssDealSellCost(dtoList);
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		return this.assTrailCarApi.deleteAddAssDealSellCostbyId(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AssDealSellCostVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		//return this.assDealSellCostApiService.modify(AssDealSellCostVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.assTrailCarApi.searchAssDealSellCostListPage(page);
		@SuppressWarnings("unchecked")
		List<AssDealSellCostDTO> dtoList = (List<AssDealSellCostDTO>) page.getResultList();
		List<AssDealSellCostVO> voList = AssDealSellCostVOTransform.toVOList(dtoList);
		this.setDic(voList);
		page.setResultList(voList);
		return page;
	}
	
	private void setDic(List<AssDealSellCostVO> voList) {
		if(CollectionUtils.isEmpty(voList))return;
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		String orgCd = CurrentThreadContext.getCurrentOrgCd();
		Map<String, String> costCdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_ASS_COST_TYP_CD);
		for (AssDealSellCostVO vo : voList) {
			if(costCdMap.containsKey(vo.getCostMainCd())){
				vo.setCostMainCdNm(costCdMap.get(vo.getCostMainCd()));
			}
			if(costCdMap.containsKey(vo.getCostSubCd())){
				vo.setCostSubCdNm(costCdMap.get(vo.getCostSubCd()));
			}
		}
	}
	
	@Override
	public List<KeyValueVO> searchCostCdListByLvl(String lvl) throws BaseException {
		if(StringUtils.isEmpty(lvl)) return null;
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroupWithLevel(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_ASS_COST_TYP_CD, Integer.valueOf(lvl));
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
		    BaseLogUtils.newLogger("获取字典项组异常 BaseException").setThrowable(e).setKeys(DictionaryConstant.CODE_ASS_COST_TYP_CD).info();
		} catch (Exception e) {
            BaseLogUtils.newLogger("获取字典项组异常 Exception").setThrowable(e).setKeys(DictionaryConstant.CODE_ASS_COST_TYP_CD).info();
		}
		return kvList;
	}

	@Override
	public List<KeyValueVO> searchCostCdListByPCode(String PCode) throws BaseException {
		if(StringUtils.isEmpty(PCode)) return null;
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchChild(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentBuOrgCd(), DictionaryConstant.CODE_ASS_COST_TYP_CD, PCode);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
            BaseLogUtils.newLogger("获取字典项组异常 BaseException").setThrowable(e).setKeys(DictionaryConstant.CODE_ASS_COST_TYP_CD).info();
		} catch (Exception e) {
            BaseLogUtils.newLogger("获取字典项组异常 Exception").setThrowable(e).setKeys(DictionaryConstant.CODE_ASS_COST_TYP_CD).info();
		}
		return kvList;
	}
}
